import 'package:flutter/material.dart';
import 'package:utils/plugins/flutter_utils.dart';
import 'package:utils/util/device_util.dart';
import 'package:utils/util/encrypt/encrypt_help.dart';
import 'package:utils/util/network_utils.dart';

import 'package:utils/util/print.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  StringBuffer msgBuffer;

  @override
  void initState() {
    super.initState();
    msgBuffer = StringBuffer();
    Print.debug = true;
    Print.fileDebug = true;
  }

  @override
  void dispose() {
    super.dispose();
    msgBuffer.clear();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          children: <Widget>[
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("判断SD卡是否存在")
                  ..write(await FlutterUtils.haveExternalStorage())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("sd卡是否存在"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得当前设备的型号：")
                  ..write(await DeviceUtil.getMode())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得当前设备的型号"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得当前运行的设备是手机还是平板：")
                  ..write(await DeviceUtil.getPhoneType())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得当前运行的设备是手机还是平板"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得语言：")
                  ..write(await DeviceUtil.getDeviceLanguage())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得语言"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得设备随机数：")
                  ..write(await DeviceUtil.getSerial())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得设备随机数"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                Print().printNative("日志测试");
              },
              child: Text("日志测试"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("Md5：")
                  ..write(EncryptHelp.generateMd5ByString("123456"))
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("123456进行md5"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                String d = EncryptHelp.encodeAES(
                    "123456", "my32lengthsupersecretnooneknows1");
                print(d);
                msgBuffer..write("AES 加密：")..write(d)..write("\n");
                this.setState(() {});
              },
              child: Text("123456进行AES加密"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                String d = EncryptHelp.decodeAES(
                    "3ed4c5191cd87c5e55d62b1a03776fa8",
                    "my32lengthsupersecretnooneknows1");
                print(d);
                msgBuffer..write("AES 解密：")..write(d)..write("\n");
                this.setState(() {});
              },
              child: Text("123456进行AES解密"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                String tmp = EncryptHelp.encodeXxtea("123456", "123456");
                msgBuffer..write("xxtea加密：")..write(tmp)..write("\n");
                this.setState(() {});
                print("xxtea加密：$tmp");
              },
              child: Text("123456进行xxtea加密，密钥：123456"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                String tmp =
                    EncryptHelp.decodeXxtea("euFqpY3zqiMSsDXk", "123456");
                msgBuffer
                  ..write("密文：euFqpY3zqiMSsDXk\n")
                  ..write("xxtea解密：")
                  ..write(tmp)
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("xxtea解密，密文：密文：XkOgRtjRe/iNSKnk，密钥：123456"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得wifi mac：")
                  ..write(await NetworkUtil.getWifiMac())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得wifi mac"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得imei：")
                  ..write(await FlutterUtils.getImei())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得imei"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得宽度：")
                  ..write(await FlutterUtils.getScreenWidth())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得宽度"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得高度：")
                  ..write(await FlutterUtils.getScreenHeight())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得高度"),
            ),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              onPressed: () async {
                msgBuffer
                  ..write("获得密度：")
                  ..write(await FlutterUtils.getScreenRatio())
                  ..write("\n");
                this.setState(() {});
              },
              child: Text("获得密度"),
            ),
            Padding(
              padding: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
              child: Text(
                msgBuffer.toString(),
                style: TextStyle(fontSize: 12),
              ),
            )
          ],
        ),
      ),
    );
  }
}
